package com.zzccyy.jpdemo.page

import android.text.TextUtils
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat
import com.zzccyy.jpdemo.R
import com.zzccyy.jpdemo.base.BasicBaseActivity
import com.zzccyy.jpdemo.bean.ZcBean
import com.zzccyy.jpdemo.dialog.BtDateDialog1
import com.zzccyy.jpdemo.utils.SPUtils
import java.text.SimpleDateFormat
import java.util.Date

class AddZcActivity : BasicBaseActivity(), View.OnClickListener {
    private var ivClose: ImageView? = null
    private var etPP: EditText? = null
    private var ll1: LinearLayout? = null
    private var ll2: LinearLayout? = null
    private var ll3: LinearLayout? = null
    private var ll4: LinearLayout? = null
    private var ll5: LinearLayout? = null
    private var tt1: TextView? = null
    private var tt2: TextView? = null
    private var tt3: TextView? = null
    private var tt4: TextView? = null
    private var tt5: TextView? = null
    private var tttDay: TextView? = null
    private var tvAdd: TextView? = null
    private var vv1: View? = null
    private var vv2: View? = null
    private var vv3: View? = null
    private var vv4: View? = null
    private var vv5: View? = null
    private var llDD: View? = null
    private var btDateDialog1: BtDateDialog1? = null


    override val layoutId: Int
        get() = R.layout.activity_add_zc

    override fun findViews() {
        ivClose = findViewById(R.id.ivClose)
        ll1 = findViewById(R.id.ll1)
        ll2 = findViewById(R.id.ll2)
        ll3 = findViewById(R.id.ll3)
        ll4 = findViewById(R.id.ll4)
        ll5 = findViewById(R.id.ll5)
        tt1 = findViewById(R.id.tt1)
        tt2 = findViewById(R.id.tt2)
        tt3 = findViewById(R.id.tt3)
        tt4 = findViewById(R.id.tt4)
        tt5 = findViewById(R.id.tt5)
        vv1 = findViewById(R.id.vv1)
        vv2 = findViewById(R.id.vv2)
        vv3 = findViewById(R.id.vv3)
        vv4 = findViewById(R.id.vv4)
        vv5 = findViewById(R.id.vv5)
        llDD = findViewById(R.id.llDD)
        tttDay = findViewById(R.id.tttDay)
        tvAdd = findViewById(R.id.tvAdd)
        etPP = findViewById(R.id.etPP)
        val sdf = SimpleDateFormat("yyyy年MM月dd日")
        val formattedDate = sdf.format(Date())
        tttDay!!.setText(formattedDate)
    }

    override fun setListener() {
        ivClose!!.setOnClickListener(this)
        ll1!!.setOnClickListener(this)
        ll2!!.setOnClickListener(this)
        ll3!!.setOnClickListener(this)
        ll4!!.setOnClickListener(this)
        ll5!!.setOnClickListener(this)
        llDD!!.setOnClickListener(this)
        tvAdd!!.setOnClickListener(this)
    }

    private var type = 1
    private var isAdd = false
    override fun onClick(v: View) {
        super.onClick(v)
        when (v.id) {
            R.id.ivClose -> finish()
            R.id.tvAdd -> {
                if (TextUtils.isEmpty(etPP!!.text.toString())) {
                    Toast.makeText(this@AddZcActivity, "请输入支出金额", Toast.LENGTH_SHORT).show()
                    return
                }
                val string = tttDay!!.text.toString()
                val year = string.substring(0, 4)
                val readList =
                    SPUtils.getInstance(this@AddZcActivity).getList("list", ZcBean::class.java)
                var i = 0
                while (i < readList.size) {
                    val zcBean = readList[i]
                    val list = zcBean.list
                    if ((zcBean.year.toString() + "") == year) {
                        val data = ZcBean.Data()
                        data.price = etPP!!.text.toString().toDouble()
                        data.type = type
                        data.time = tttDay!!.text.toString()
                        zcBean.year = year.toInt()
                        list!!.add(data)
                        isAdd = true
                    }
                    i++
                }
                if (!isAdd) {
                    val zcBean = ZcBean()
                    val data = ArrayList<ZcBean.Data>()
                    val data1 = ZcBean.Data()
                    data1.type = type
                    data1.price = etPP!!.text.toString().toDouble()
                    data1.time = tttDay!!.text.toString()
                    data.add(data1)
                    zcBean.year = year.toInt()
                    zcBean.list=data
                    readList.add(zcBean)
                }
                SPUtils.getInstance(this@AddZcActivity).putList("list", readList)
                setResult(RESULT_OK)
                finish()
            }

            R.id.llDD -> {
                if (btDateDialog1 == null) {
                    btDateDialog1 = BtDateDialog1(
                        this@AddZcActivity,
                        object : BtDateDialog1.OnViewClickListener {
                            override fun setData(data: String?) {
                                tttDay!!.text = data
                            }
                        })
                }
                btDateDialog1!!.show()
            }

            R.id.ll1 -> {
                type = 1
                ll1!!.setBackgroundResource(R.drawable.bg_5a79ff_c15)
                ll2!!.setBackgroundResource(R.drawable.bg_ffffff_c15)
                ll3!!.setBackgroundResource(R.drawable.bg_ffffff_c15)
                ll4!!.setBackgroundResource(R.drawable.bg_ffffff_c15)
                ll5!!.setBackgroundResource(R.drawable.bg_ffffff_c15)
                tt1!!.setTextColor(ContextCompat.getColor(this@AddZcActivity, R.color.color_ffffff))
                tt2!!.setTextColor(ContextCompat.getColor(this@AddZcActivity, R.color.color_000000))
                tt3!!.setTextColor(ContextCompat.getColor(this@AddZcActivity, R.color.color_000000))
                tt4!!.setTextColor(ContextCompat.getColor(this@AddZcActivity, R.color.color_000000))
                tt5!!.setTextColor(ContextCompat.getColor(this@AddZcActivity, R.color.color_000000))
                vv1!!.visibility = View.VISIBLE
                vv2!!.visibility = View.GONE
                vv3!!.visibility = View.GONE
                vv4!!.visibility = View.GONE
                vv5!!.visibility = View.GONE
            }

            R.id.ll2 -> {
                type = 2
                ll2!!.setBackgroundResource(R.drawable.bg_5a79ff_c15)
                ll1!!.setBackgroundResource(R.drawable.bg_ffffff_c15)
                ll3!!.setBackgroundResource(R.drawable.bg_ffffff_c15)
                ll4!!.setBackgroundResource(R.drawable.bg_ffffff_c15)
                ll5!!.setBackgroundResource(R.drawable.bg_ffffff_c15)
                tt2!!.setTextColor(ContextCompat.getColor(this@AddZcActivity, R.color.color_ffffff))
                tt1!!.setTextColor(ContextCompat.getColor(this@AddZcActivity, R.color.color_000000))
                tt3!!.setTextColor(ContextCompat.getColor(this@AddZcActivity, R.color.color_000000))
                tt4!!.setTextColor(ContextCompat.getColor(this@AddZcActivity, R.color.color_000000))
                tt5!!.setTextColor(ContextCompat.getColor(this@AddZcActivity, R.color.color_000000))
                vv2!!.visibility = View.VISIBLE
                vv1!!.visibility = View.GONE
                vv3!!.visibility = View.GONE
                vv4!!.visibility = View.GONE
                vv5!!.visibility = View.GONE
            }

            R.id.ll3 -> {
                type = 3
                ll3!!.setBackgroundResource(R.drawable.bg_5a79ff_c15)
                ll1!!.setBackgroundResource(R.drawable.bg_ffffff_c15)
                ll2!!.setBackgroundResource(R.drawable.bg_ffffff_c15)
                ll4!!.setBackgroundResource(R.drawable.bg_ffffff_c15)
                ll5!!.setBackgroundResource(R.drawable.bg_ffffff_c15)
                tt3!!.setTextColor(ContextCompat.getColor(this@AddZcActivity, R.color.color_ffffff))
                tt2!!.setTextColor(ContextCompat.getColor(this@AddZcActivity, R.color.color_000000))
                tt1!!.setTextColor(ContextCompat.getColor(this@AddZcActivity, R.color.color_000000))
                tt4!!.setTextColor(ContextCompat.getColor(this@AddZcActivity, R.color.color_000000))
                tt5!!.setTextColor(ContextCompat.getColor(this@AddZcActivity, R.color.color_000000))
                vv3!!.visibility = View.VISIBLE
                vv2!!.visibility = View.GONE
                vv1!!.visibility = View.GONE
                vv4!!.visibility = View.GONE
                vv5!!.visibility = View.GONE
            }

            R.id.ll4 -> {
                type = 4
                ll4!!.setBackgroundResource(R.drawable.bg_5a79ff_c15)
                ll1!!.setBackgroundResource(R.drawable.bg_ffffff_c15)
                ll2!!.setBackgroundResource(R.drawable.bg_ffffff_c15)
                ll3!!.setBackgroundResource(R.drawable.bg_ffffff_c15)
                ll5!!.setBackgroundResource(R.drawable.bg_ffffff_c15)
                tt4!!.setTextColor(ContextCompat.getColor(this@AddZcActivity, R.color.color_ffffff))
                tt2!!.setTextColor(ContextCompat.getColor(this@AddZcActivity, R.color.color_000000))
                tt3!!.setTextColor(ContextCompat.getColor(this@AddZcActivity, R.color.color_000000))
                tt1!!.setTextColor(ContextCompat.getColor(this@AddZcActivity, R.color.color_000000))
                tt5!!.setTextColor(ContextCompat.getColor(this@AddZcActivity, R.color.color_000000))
                vv4!!.visibility = View.VISIBLE
                vv2!!.visibility = View.GONE
                vv3!!.visibility = View.GONE
                vv1!!.visibility = View.GONE
                vv5!!.visibility = View.GONE
            }

            R.id.ll5 -> {
                type = 5
                ll5!!.setBackgroundResource(R.drawable.bg_5a79ff_c15)
                ll1!!.setBackgroundResource(R.drawable.bg_ffffff_c15)
                ll2!!.setBackgroundResource(R.drawable.bg_ffffff_c15)
                ll3!!.setBackgroundResource(R.drawable.bg_ffffff_c15)
                ll4!!.setBackgroundResource(R.drawable.bg_ffffff_c15)
                tt5!!.setTextColor(ContextCompat.getColor(this@AddZcActivity, R.color.color_ffffff))
                tt2!!.setTextColor(ContextCompat.getColor(this@AddZcActivity, R.color.color_000000))
                tt3!!.setTextColor(ContextCompat.getColor(this@AddZcActivity, R.color.color_000000))
                tt4!!.setTextColor(ContextCompat.getColor(this@AddZcActivity, R.color.color_000000))
                tt1!!.setTextColor(ContextCompat.getColor(this@AddZcActivity, R.color.color_000000))
                vv5!!.visibility = View.VISIBLE
                vv2!!.visibility = View.GONE
                vv3!!.visibility = View.GONE
                vv4!!.visibility = View.GONE
                vv1!!.visibility = View.GONE
            }
        }
    }


    override fun initData() {
    }
}